package io.github.jsp.dao;

import io.github.jsp.model.JobRegistry;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface JobRegistryMapper {
    
    @Select("SELECT * FROM sys_job_registry WHERE id = #{id}")
    JobRegistry selectById(Integer id);
    
    @Select("SELECT * FROM sys_job_registry WHERE registry_group = #{registryGroup} ORDER BY update_time DESC")
    List<JobRegistry> selectByGroup(String registryGroup);
    
    @Select("SELECT * FROM sys_job_registry WHERE registry_group = #{registryGroup} AND registry_key = #{registryKey}")
    JobRegistry selectByGroupAndKey(@Param("registryGroup") String registryGroup, @Param("registryKey") String registryKey);
    
    @Insert("INSERT INTO sys_job_registry (registry_group, registry_key, registry_value, update_time) " +
            "VALUES (#{registryGroup}, #{registryKey}, #{registryValue}, CURRENT_TIMESTAMP) " +
            "ON CONFLICT (registry_group, registry_key) " +
            "DO UPDATE SET registry_value = EXCLUDED.registry_value, update_time = CURRENT_TIMESTAMP")
    int insertOrUpdate(JobRegistry jobRegistry);
    
    @Delete("DELETE FROM sys_job_registry WHERE registry_group = #{registryGroup} AND registry_key = #{registryKey}")
    int deleteByGroupAndKey(@Param("registryGroup") String registryGroup, @Param("registryKey") String registryKey);
    
    @Delete("DELETE FROM sys_job_registry WHERE update_time < #{beforeTime}")
    int deleteBeforeTime(@Param("beforeTime") java.sql.Timestamp beforeTime);
}